﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace NS_Client
{
    public partial class MainForm : NS_UI
    {
        MainController MainControl;
        SessionModel LastSessionUpdate;

        //CreateContentForm CreateContentDlg;

        public MainForm()
        {
            InitializeComponent();
        }

        /// <summary>Sets the ILoginFormController</summary>
        /// <param name="controller">Must be inherited from IFormController</param>
        public override void SetListener(ViewController controller)
        {
            this.MainControl = (MainController)controller;
        }

        /// <summary>Notifies the form to update based of new state</summary>
        /// <param name="Data">The data used to update the form</param>
        public override void notify(object Data)
        {
            LastSessionUpdate = (SessionModel)Data;

            ContentTableBinding.DataSource = LastSessionUpdate.ContentItems;
            ContentTableBinding.ResetBindings(false);

            lblUser.Text = LastSessionUpdate.Username;
        }

        void ContentTable_CellClick(object sender, System.Windows.Forms.DataGridViewCellEventArgs e)
        {
            // Ignore clicks that are not on button cells. 
            if (e.RowIndex < 0 || e.ColumnIndex !=
                ContentTable.Columns["ContentTable_DownloadBtnCol"].Index) return;

            System.Diagnostics.Debug.WriteLine("hia");

            // Retrieve the Corresponding Content Name object from the "Name" cell.
            string CorrespondingContentID = ContentTable.Rows[e.RowIndex].Cells[ID_DataGridViewTextBoxColumn.Index].Value as string;

            MainControl.Download(CorrespondingContentID);
        }

        private void MainForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            MainControl.CloseView();
        }

        private void viewContentToolStripMenuItem_Click(object sender, EventArgs e)
        {
            tabControl.SelectTab(viewContent);
        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            OpenFileDialog FileBrowser = new OpenFileDialog();
            FileBrowser.ShowDialog();
            txtFileLocation.Text = FileBrowser.FileName;
        }

        private void btnCreate_Click(object sender, EventArgs e)
        {
            MainControl.CreateContent(txtContentName.Text, txtContentDesc.Text, txtFileLocation.Text);
        }

        private void MenuItem_CreateContent_Click(object sender, EventArgs e)
        {
           
        }

        private void btn_SendChat_Click(object sender, EventArgs e)
        {
            MainControl.SendChatMsg(txt_SendChat.Text);
            txt_SendChat.Text = "";
        }

        private void chat_Tmr_Tick(object sender, EventArgs e)
        {
            MainControl.PollChat();
        }
    }
}
